﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0657 //2010.12.08 //Begin
namespace InfoJetSoft.Service.Worker
{
    //这里假设PGPicker必须使用统一的默认前缀。
    class XSNViewFormatEXIPickerWorker
    {

        public static void formatEXIPicker(View view)
        {
            XmlElement root = view.Doc.DocumentElement;
            string xdBindingName = NamespaceUtil.toFullName(view.XdPrefix, "binding");

            //CID 0828 //2011.03.10 //Begin
            int index = 0;
            //CID 0828 //2011.03.10 //End

            string objectFilter = "//object[@classid='clsid:ad74fc20-e09f-4e47-8a87-1da49930867a']";
            string boxFilter = "table/tr/td/div";
            string imgFilter = "table/tr/td/button/img";
            //CID 0820 //2011.03.30 //Begin
            string attributeFilter = NamespaceUtil.toFullName(view.XslPrefix, "attribute");
            string chooseFilter = NamespaceUtil.toFullName(view.XslPrefix, "choose");
            string whenAttributeFilter = NamespaceUtil.toFullName(view.XslPrefix, "when") + "/" + NamespaceUtil.toFullName(view.XslPrefix, "attribute");
            //CID 0820 //2011.03.30 //End
            XmlNodeList objectElementList = DOMUtil.selectNodes(root, objectFilter, 0, view.NsResolver);
            foreach (XmlElement objectElement in objectElementList)
            {
                if (view.Manifest.EXIInfoTable == null)
                {
                    view.Manifest.EXIInfoTable = new Hashtable();
                }
                EXIInfo exiInfo = parseEXIInfo(objectElement);
                string id = view.Manifest.EXIInfoTable.Count.ToString();
                view.Manifest.EXIInfoTable.Add(id, exiInfo);

                string binding = objectElement.GetAttribute(xdBindingName);
                if (binding != null && binding.Length > 0)
                {
                    XmlDocument divTempDoc = new XmlDocument();
                    string divXml = templateXml.Replace(":binding:", binding);
                    divXml = divXml.Replace(":delete.gif:", view.Manifest.ImageUri.Delete);
                    divTempDoc.LoadXml(divXml);

                    XmlElement divElement = view.Doc.ImportNode(divTempDoc.DocumentElement, true) as XmlElement;

                    //CID 0820 //2011.03.30 //Begin
                    XmlElement chooseElement = DOMUtil.selectANode(objectElement, chooseFilter, view.NsResolver) as XmlElement;
                    if (chooseElement != null)
                    {
                        XmlElement attrElement = DOMUtil.selectOneNode(chooseElement, whenAttributeFilter, view.NsResolver) as XmlElement;
                        if (attrElement.GetAttribute("name").EndsWith("disableEditing"))
                        {
                            attrElement.SetAttribute("name", "disabled");
                        }
                        else
                        {
                            throw new InfoJetException("Unexcepted " + attrElement.GetAttribute("name"), "Picker.disableEditing");
                        }
                        divElement.InsertBefore(chooseElement, divElement.FirstChild);
                    }
                    XmlElement attributeElement = DOMUtil.selectANode(objectElement, attributeFilter, view.NsResolver) as XmlElement;
                    if (attributeElement != null)
                    {
                        divElement.InsertBefore(attributeElement, divElement.FirstChild);
                    }
                    //CID 0820 //2011.03.30 //End

                    XmlElement boxElement = divElement.SelectSingleNode(boxFilter) as XmlElement;
                    boxElement.AppendChild(view.Doc.CreateTextNode(System.Convert.ToChar(0xA0).ToString()));
                    XmlElement imgElement = divElement.SelectSingleNode(imgFilter) as XmlElement;
                    imgElement.SetAttribute("src", view.Manifest.ImageUri.AddressBook);

                    //CID 2011.03.10 //Begin
                    boxElement.SetAttribute(JetConstant.HTML_id, "InfoJet_EXI" + index);
                    index++;
                    //CID 2011.03.10 //End

                    XmlElement trElement = boxElement.ParentNode.ParentNode as XmlElement;
                    //CID 0858 //2011.03.29 //Begin
                    trElement.SetAttribute(JetConstant.HTML_id, DOMUtil.getControlBindingExpr(view, binding));
                    trElement.SetAttribute("isPicker", "true");
                    //CID 0858 //2011.03.29 //End
                    trElement.SetAttribute("exiId", id);
                    trElement.SetAttribute("multiItem", exiInfo.MultiItem);
                    //CID 0748 //2010.12.29 //Begin
                    trElement.SetAttribute("pickerDialogTitle", exiInfo.PickerDialogTitle);
                    //CID 0748 //2010.12.29 //End

                    XmlElement parentElement = objectElement.ParentNode as XmlElement;
                    parentElement.ReplaceChild(divElement, objectElement);
                    string style = objectElement.GetAttribute("style");
                    if (style != null)
                    {
                        divElement.SetAttribute("style", style);
                    }
                }
            }
        }

        private static EXIInfo parseEXIInfo(XmlElement objectElement)
        {
            EXIInfo exiInfo = new EXIInfo();
            XmlNodeList paramElementList = objectElement.SelectNodes("param");
            foreach (XmlElement paramElement in paramElementList)
            {
                string name = paramElement.GetAttribute("NAME");
                if (name.Equals("EntityNamespace"))
                {
                    exiInfo.EntityNamespace = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("EntityName"))
                {
                    exiInfo.EntityName = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("EntityFinderName"))
                {
                    exiInfo.EntityFinderName = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("DisplayFieldName"))
                {
                    exiInfo.DisplayFieldName = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("SystemInstanceName"))
                {
                    exiInfo.SystemInstanceName = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("EntityBDCServerURL"))
                {
                    exiInfo.EntityBDCServerURL = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("PickerDialogTitle"))
                {
                    exiInfo.PickerDialogTitle = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("RefreshOnOpen"))
                {
                    exiInfo.RefreshOnOpen = paramElement.GetAttribute("VALUE");
                }
                else if (name.Equals("MultiItem"))
                {
                    exiInfo.MultiItem = paramElement.GetAttribute("VALUE");
                }
                //CID 0740 //2010.12.27 //Begin
                else if (name.Equals("MaxResults"))
                {
                    exiInfo.MaxResultsValue = paramElement.GetAttribute("VALUE");
                }
                //CID 0740 //2010.12.27 //End
            }
            //CID 0740 //2010.12.27 //Begin
            if (exiInfo.MaxResultsValue != null)
            {
                int maxResults = 0;
                if (int.TryParse(exiInfo.MaxResultsValue, out maxResults))
                {
                    exiInfo.MaxResults = maxResults;
                }
                else
                {
                    exiInfo.MaxResults = 200;
                }
            }
            else
            {
                exiInfo.MaxResults = 200;
            }
            //CID 0740 //2010.12.27 //End
            return exiInfo;
        }

        //2013.3.3 sunny modified disable the buttons
        
//        //CID 0828 //2011.03.10 //Begin
//        private static string templateXml = @"
//<span xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
//<table style='width:100%'>
//<tr>
//<td width='92%'><div class='xdRichTextBox' style='width:100%;height:100%' isItemsContainer='true'>
//<xsl:for-each select=':binding:/pc:BDCAssociatedEntity/pc:BDCEntity'>
//        <xsl:if test='string-length(pc:EntityId1)'>
//            <a href='javascript:void(0);' png='true' onclick='return InfoJet_OnEXIItemClick(this);' onkeydown='return InfoJet_OnEXIItemKeyDown(event,this);' onblur='InfoJet_OnEXIItemBlur(this);'>
//		        <xsl:attribute name='style'>
//	                display:inline-block;cursor:default;color:#000000;
//                </xsl:attribute>
//                <xsl:value-of select='pc:EntityDisplayName'/>
//                <img src=':delete.gif:' style='display:none;width:12px;height:12px;border:none;' onclick='InfoJet_OnEXIItemDelete(this);'>
//                    <xsl:attribute name='entityId1'>
//                        <xsl:value-of select='pc:EntityId1'/>
//                    </xsl:attribute>
//                    <xsl:attribute name='entityId2'>
//                        <xsl:value-of select='pc:EntityId2'/>
//                    </xsl:attribute>
//                    <xsl:attribute name='entityId3'>
//                        <xsl:value-of select='pc:EntityId3'/>
//                    </xsl:attribute>
//                    <xsl:attribute name='entityId4'>
//                        <xsl:value-of select='pc:EntityId4'/>
//                    </xsl:attribute>
//                    <xsl:attribute name='entityId5'>
//                        <xsl:value-of select='pc:EntityId5'/>
//                    </xsl:attribute>
//                </img>
//	        </a>
//            <span>; </span>
//        </xsl:if>
//</xsl:for-each>
//</div></td>
//<td algin='right'><button style='height:20px;width:21px;border:none;background-color:white;' onclick='InfoJet_OnEXIPickerClick(event, this);'><img src=''/></button></td>
//</tr>
//</table>
//</span>";

//        //CID 0828 //2011.03.10 //End
        private static string templateXml = @"
<span xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<table style='width:100%'>
<tr>
<td width='92%'><div class='xdRichTextBox' style='width:100%;height:100%' isItemsContainer='true'>
<xsl:for-each select=':binding:/pc:BDCAssociatedEntity/pc:BDCEntity'>
        <xsl:if test='string-length(pc:EntityId1)'>
            <a href='javascript:void(0);' png='true' onclick='' onkeydown='' onblur=''>
		        <xsl:attribute name='style'>
	                display:inline-block;cursor:default;color:#000000;
                </xsl:attribute>
                <xsl:value-of select='pc:EntityDisplayName'/>
                <img src=':delete.gif:' style='display:none;width:12px;height:12px;border:none;' onclick=''>
                    <xsl:attribute name='entityId1'>
                        <xsl:value-of select='pc:EntityId1'/>
                    </xsl:attribute>
                    <xsl:attribute name='entityId2'>
                        <xsl:value-of select='pc:EntityId2'/>
                    </xsl:attribute>
                    <xsl:attribute name='entityId3'>
                        <xsl:value-of select='pc:EntityId3'/>
                    </xsl:attribute>
                    <xsl:attribute name='entityId4'>
                        <xsl:value-of select='pc:EntityId4'/>
                    </xsl:attribute>
                    <xsl:attribute name='entityId5'>
                        <xsl:value-of select='pc:EntityId5'/>
                    </xsl:attribute>
                </img>
	        </a>
            <span>; </span>
        </xsl:if>
</xsl:for-each>
</div></td>
<td algin='right'><button style='height:20px;width:21px;border:none;background-color:white;' onclick=''><img src=''/></button></td>
</tr>
</table>
</span>";
        //2013.3.3 sunny end
    }
}
//CID 0657 //2010.12.15 //End